app.controller("invoiceSale",function($scope,$uibModal,$rootScope,del,$state,get,$stateParams,Upload ){
	//返回
	$scope.rement=function(){
		$state.go("home."+$stateParams.url,{page:$stateParams.page},{
						     		reload:true
						     	})
	}
	$scope.cid=$stateParams.cid
	$scope.search=function(i){
		var param={
			pageNum:i||'1',
			pageSize:8,
			cid:$stateParams.cid,
			key:$scope.key||null,
		}
		get.list("invoice/all",param).then(function(e){
			$scope.list=e.list
			$scope.pageSize = e.pageSize; //每页多少条
			$scope.totalNum = e.pageCount; //总共多少页
			$scope.totalSize =e.totalNums; //总共多少条   
			$scope.pageNum = e.pageNum; //当前页面
		})
		
   	
		
	
	}


	$scope.pageUp=function(){//跳转页码
       	if ($scope.tj.page>$scope.totalNum) {
       		$scope.tj.page=$scope.totalNum
       	} else if($scope.tj.page<1){
       	$scope.tj.page=null
       	}
       }//pageUp
   $scope.toPage=function(i){//点击跳转
       i=i||$scope.tj.page
       	$scope.search(i)
       	$scope.tj.current=$scope.tj.page;//页码调整
   }//toPage
   if($stateParams.page!=null){
			$scope.tj={
				page:$stateParams.page
			}
		 $scope.toPage($stateParams.page)
	}else{
			$scope.search();
	}
   /**
	 * 删除
	 */
	$scope.modelClick = function(id) { //单删
	$uibModal.open({ //模态框
		animation: true,
		templateUrl: 'myModalDel.html',
		size: 'sm',
		 backdrop: 'static' ,
		controller: function($uibModalInstance) {
			var $ctrl = this;
				$ctrl.state = true;
			$ctrl.ok = function() {
				del.list("invoice/del",{ipId:id}).then(function(e){
					$uibModalInstance.close();
					$rootScope.open(e);
					$scope.search($scope.pageNum)
				});
				
			}; //ok  
			$ctrl.cancel = function() {
				$uibModalInstance.close();
			};
		}, //controller
		controllerAs: '$ctrl'
	})
	}
	
	
/**
	 * 增加与修改
	 * @param {Object} id
	 * @param {Object} tab
	 */
	$scope.columnAdd = function(id,tab) {
		$uibModal.open({ //模态框
			animation: true,
			templateUrl: 'invoiceSaleAdd.html',
			size: 'lg',
			 backdrop: 'static' ,
			controller: 'invoiceSaleAdd',
			resolve: { //这是一个入参,这个很重要,它可以把主控制器中的参数传到模态框控制器中
				id: function() { //是一个回调函数
					return id; //这个值会被模态框的控制器获取到
				},
				tab:function (){
					return tab;
				},
				cid:function (){
					return $scope.cid;
				},
				scope:function(){
					return $scope;
				}
			}
		}) //模态框
	}
})
app.controller('invoiceSaleAdd',function($scope,$rootScope,get,ngTreetableParams,$uibModalInstance,tab,id,$http,post,scope,$state,cid){
	$scope.id=id
	$scope.cid=cid
	$scope.param=[]
	$scope.param.ipZtype=1
	if(tab=='add'){
		$scope.param.cpType=''
		$scope.sername="添加";
		$scope.param.cZtype=1
	}else{
		$scope.sername="修改";
		get.list("invoice/findOne",{ipId:$scope.id}).then(function(e){
			$scope.param.ipId=e.ipId
			$scope.param.cId=e.cId
			$scope.param.prAcount=e.prAcount
			$scope.param.dateTime=e.prPostTime
			$scope.pickertime=e.prPostTime
			
		})
	}
	if($scope.id==0||$scope.href==''){
		$scope.type="menu"
	}else{
		$scope.type="permission"
	}
	$scope.ok=function (){
		$scope.param.dateTime=$scope.pickertime||$scope.param.dateTime
		$scope.param.cId=$scope.cid
		if(tab=='add'){
			post.list("invoice/add",$scope.param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			scope.search()
		});
		}else{
			$scope.param.cType||''
		post.list("invoice/update",
			$scope.param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			scope.search()
			
		});
		}
	}
	$scope.cancel = function() {
		$uibModalInstance.close();
	};
	
});
